// pages/mine/mine.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guild_name: null,
    check_status: null,
    status: null,
    re_number: '',
    is_perfect: '',

    avatar_url: null,
    nickname: null,
    signature: null,
    sex: null,
    role: null,
    //模态框
    flag_float: true,
    flag_release: true,

    //收藏数、沟通数
    info: {},
    collect_total: '',
    is_linkup_total: '',
    anchor_send_resume: '', //职位投递总数
    send_position_total: '', //发布职位数
    is_read: '', //职位投递未读总数

  },
  getTab() {
    wx.request({
      url: app.globalData.url + 'api/timing/applet/show',
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let is_show = e.data.data.is_show,
          tel = wx.getStorageSync('reg_telephone');
        this.setData({
          is_show: is_show,
        })
        // if (tel == '18258151021') {
        if (is_show == app.globalData.tab_is_show) {
          this.setData({
            tel: tel
          })
          this.setData({
            tabbar: {
              color: "#fff",
              selectedColor: "#d0a76c",
              backgroundColor: "#1b1b1b",
              borderStyle: "black",
              list: [{
                  pagePath: "../../pages/industry/industry",
                  text: "行业",
                  iconPath: "../../images/icon/icon_ind.png",
                  selectedIconPath: "../../images/icon/icon_indS.png",
                  selected: false
                },
                {
                  pagePath: "../../pages/mine/mine",
                  text: "我的",
                  iconPath: "../../images/icon/icon_mine.png",
                  selectedIconPath: "../../images/icon/icon_mineS.png",
                  selected: true
                }
              ],
              position: "bottom"
            },
          })
          this.editTabBar();
        } else {
          if (this.data.role == 2) {
            this.setData({
              tabbar: {
                color: "#fff",
                selectedColor: "#d0a76c",
                backgroundColor: "#1b1b1b",
                borderStyle: "black",
                list: [{
                    pagePath: "../../pages/recruit/index/index",
                    text: "招聘",
                    iconPath: "../../images/icon/icon_rec.png",
                    selectedIconPath: "../../images/icon/icon_recS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/earns/earns",
                    text: "赚钱",
                    iconPath: "../../images/icon/icon_money.png",
                    selectedIconPath: "../../images/icon/icon_moneyS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/industry/industry",
                    text: "行业",
                    iconPath: "../../images/icon/icon_ind.png",
                    selectedIconPath: "../../images/icon/icon_indS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/mine/mine",
                    text: "我的",
                    iconPath: "../../images/icon/icon_mine.png",
                    selectedIconPath: "../../images/icon/icon_mineS.png",
                    selected: true
                  }
                ],
                position: "bottom"
              },
            })
          } else {
            this.setData({
              tabbar: {
                color: "#fff",
                selectedColor: "#d0a76c",
                backgroundColor: "#1b1b1b",
                borderStyle: "black",
                list: [{
                    pagePath: "../../pages/recruit/index/index",
                    text: "招聘",
                    iconPath: "../../images/icon/icon_rec.png",
                    selectedIconPath: "../../images/icon/icon_recS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/industry/industry",
                    text: "行业",
                    iconPath: "../../images/icon/icon_ind.png",
                    selectedIconPath: "../../images/icon/icon_indS.png",
                    selected: false
                  },
                  {
                    pagePath: "../../pages/mine/mine",
                    text: "我的",
                    iconPath: "../../images/icon/icon_mine.png",
                    selectedIconPath: "../../images/icon/icon_mineS.png",
                    selected: true
                  }
                ],
                position: "bottom"
              },
            })
          }

          this.editTabBar();
        }
      },
    })
  },

  //切换tabbar的方法  
  editTabBar: function() {
    var tabbar = this.data.tabbar,
      currentPages = getCurrentPages(),
      _this = currentPages[currentPages.length - 1],
      pagePath = _this.__route__;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (var i in tabbar.list) {
      // tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
    // console.log(tabbar)
  },

  // 加载高度宽度
  loadImg(e) {
    console.log(e)
    let imgWidth = e.detail.width,
      imgHeight = e.detail.height;
    this.setData({
      imgWidth: imgWidth,
      imgHeight: imgHeight,
    })
  },
  //主播投递的职位
  click_anchorDeliver() {
    wx.navigateTo({
      url: 'anchorDeliver/index',
    })
  },
  //去播豆
  go_bodou() {
    wx.navigateTo({
      url: '../payment/bodou/index',
    })
  },
  //去vip页面
  go_vip() {
    wx.navigateTo({
      url: 'vip/index',
    })
  },
  // 公司信息是否完善
  iscompany() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token,
        type: 2,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let is_perfect = e.data.data.company.is_perfect ? e.data.data.company.is_perfect : "";
        that.setData({
          is_perfect: is_perfect,
        })
      },
    })
  },
  //去主播收藏
  click_anchor_col() {
    wx.navigateTo({
      url: 'CollectHost/CollectHost',
    })
  },
  //去公司主页 
  go_company() {
    console.log(this.data.is_perfect)
    // if (this.data.is_perfect==1){
    wx.navigateTo({
      url: 'CompanyHomepage/CompanyHomepage',
    })
    // }else{
    //   wx.showModal({
    //     content: '请先完善公司信息',
    //     showCancel: true,
    //     cancelText: '取消',
    //     cancelColor: '#333',
    //     confirmText: '好的',
    //     confirmColor: '#d0a76c',
    //     success: function (e) {
    //       if (e.confirm) {
    //         wx.navigateTo({
    //           url: '../recruit/CompanyProcess/CompanyProcess',
    //         })
    //       }
    //     },
    //   })
    // }

  },
  //去任务页面
  go_task(e) {
    // console.log(e)
    let type = e.currentTarget.dataset.type;
    if (type == 1) {
      //签到
      wx.navigateTo({
        url: 'task/index?show=show_check',
      })
    } else {
      //任务
      wx.navigateTo({
        url: 'task/index?show=show_task',
      })
    }

  },
  // 去H5通用页
  go_protocol(e) {
    console.log(e)
    let url = e.currentTarget.dataset.url,
      title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: 'protocol/protocol?url=' + url + '&title=' + title
    })
  },
  //去邀请好友
  go_invite() {
    if (this.data.role == 2) {
      wx.navigateTo({
        url: 'invite/index',
        // url: '../boMoney/index',
      })
    }

  },
  //七日收益
  earns() {
    wx.request({
      url: app.globalData.url + 'api/task/seven/day/earnings',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let earnsInfo = e.data.data.earnings;
        var xInfo = [],
          yInfo = [];
        earnsInfo.forEach(function(value, index) {
          var str = value.show_date;
          var str_new = str.replace(/"-"/, ".");
          var str_re = str_new.substring(5);
          value.show_date = str_re;
          value.operate_num = parseInt(value.operate_num);
          xInfo.push(value.show_date);
          yInfo.push(value.operate_num)
        })
        // outSide.xInfo = xInfo;
        // outSide.yInfo = yInfo;
        wx.setStorageSync('xInfo', xInfo)
        wx.setStorageSync('yInfo', yInfo)

        // console.log(outSide.xInfo)
        // console.log(outSide.yInfo)
      }
    })
  },
  //去主播升级页
  go_update() {
    wx.navigateTo({
      url: 'anchorWelfare/upgrade/upgrade'
    })
  },
  //去主播福利页
  go_anchorWelfare() {
    this.earns();
    if (this.data.role == 2) {
      wx.navigateTo({
        url: 'anchorWelfare/anchorWelfareRe',
        // url: '../boMoney/index',
      })
    } else if (this.data.role == 1) {
      wx.navigateTo({
        url: 'presidentEarnsRe/presidentEarnsRe',
      })
    }
  },
  //去会长福利页
  go_presidentWelfare() {
    let that = this;
    wx.navigateTo({
      url: 'presidentWelfare/presidentWelfare',
    })
  },
  //去消息页
  click_message() {
    wx.navigateTo({
      url: 'message/message',
    })
  },

  //去购买
  go_payment() {
    wx.navigateTo({
      url: 'residueTime/residueTime',
    })
  },
  //去设置页面
  go_setting() {
    wx.navigateTo({
      url: 'setting/setting',
    })
  },
  //去修改个人资料页
  click_modify() {
    wx.navigateTo({
      url: '../mine/modifyInfor/modifyInfor',
    })
  },
  //会长已经沟通
  click_already_talk() {
    wx.navigateTo({
      url: '../recruit/alreadyTalk/alreadyTalk',
    })
  },
  //去主播投递
  click_delivery() {
    wx.navigateTo({
      url: '../recruit/deliveryList/deliveryList',
    })
  },
  //去已经沟通页面
  click_communicate: function() {
    wx.navigateTo({
      url: '../mine/communicate/communicate',
    })
  },
  //发布职位
  click_release: function() {
    if (this.data.role != "3") {
      wx.navigateTo({
        url: '../recruit/jobList/jobList',
      })
    } else {
      this.setData({
        flag_release: false,
      })
    }
  },
  // 模态框全部关闭
  modal_hide: function() {
    this.setData({
      flag_float: true,
      flag_release: true,
    })
  },
  //模态框 拨打
  click_phone: function() {
    //管理员、会长
    wx.makePhoneCall({
      phoneNumber: '0571-87081736',
    })
    this.setData({
      flag_contact: true,
    })
  },
  //模态框 复制
  click_modal_copy: function() {
    wx.setClipboardData({
      data: 'zhibozhijia888',
      success: function(e) {}
    })
  },
  //形象卡
  click_card: function(e) {
    // console.log(e);
    if (this.data.info.register_info.resume_id) {
      wx.navigateTo({
        url: '../resume/singleShow/singleShow',
      })
    } else {
      wx.navigateTo({
        url: '../resume/uploadResume/uploadResume?title=我的形象',
      })
    }

  },

  go_collection: () => {
    wx.navigateTo({
      url: '../mine/collection/collection',
    })
  },
  go_guildManage: () => {
    wx.navigateTo({
      url: '../mine/guildManage/guildManage',
    })
  },
  go_anchorBind: () => {
    wx.navigateTo({
      url: '../mine/anchorBind/anchorBind',
    })
  },
  go_aboutUs: () => {
    wx.navigateTo({
      url: '../mine/aboutUs/aboutUs',
    })
  },
  go_senior: () => {
    wx.navigateTo({
      url: '../mine/senior/senior',
    })
  },
  go_service: () => {
    wx.navigateTo({
      url: '../mine/service/index',
    })
  },

  //用户基础信息接口
  user_info: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        var info = e.data.data;
        var endtime = parseInt((info.expire_time - app.globalData.sjcNow) / 86400)
        // info.forEach(function(value, index, ) {
        //   value.endtime = parseInt((value.expire_time - app.globalData.sjcNow) / 86400)
        // })
        that.setData({
          info: info,
          endtime: endtime,
          collect_total: e.data.data.collect_total, //职位收藏数
          is_linkup_total: e.data.data.is_linkup_total, //已沟通数
          anchor_send_resume: e.data.data.anchor_send_resume, //职位投递总数
          send_position_total: e.data.data.send_position_total, //发布职位数
          is_read: e.data.data.is_read, //职位投递未读总数
        })
      },

    })
  },
  //guild
  getGuild() {
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/mine/guild',
      data: {
        token: app.globalData.token
      },
      success: (e) => {
        // console.log(e.data.data.guilds)
        // console.log(e.data.data.guilds[0].check_status)
        // var status = e.data.data.guilds[0].check_status;
        var old_guilds = e.data.data.guilds;
        var guild_name = [];
        for (var i = 0; i < old_guilds.length; i++) {
          // console.log(i)
          if (old_guilds[i].check_status == 1) {
            guild_name.push(old_guilds[i])
          }
        }
        this.setData({
          check_status: e.data.data,
          guild_name: guild_name
        })
      }
    })
  },
  //埋点
  secret() {
    wx.request({
      url: app.globalData.url + 'api/timing/common/oncilck/bottom',
      data: {
        token: app.globalData.token,
        from: 'WxMiNi',
        type: '4'
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getTab();
    this.setData({
      tab_is_show: app.globalData.tab_is_show
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.globalData.token) {
      this.user_info();
      this.iscompany();
      this.earns();
      this.secret();
    }
    // this.user_info();
    // this.iscompany();
    wx.showLoading({
      title: '小主稍等~',
    })
    this.setData({
      role: app.register_info.register_role
    });
    wx.hideLoading()
    //授权检测reg_register_role
    if (!(wx.getStorageSync('token'))) {
      wx.redirectTo({
        url: '../author/author',
      })
      // console.log('跳转')
    } else {
      if (wx.getStorageSync('reg_register_role') == 0) {
        wx.redirectTo({
          url: '../selectIdentity/selectIdentity',
        })
        // wx.showModal({
        //   content: '选择角色后才能看详情哦~',
        //   showCancel: false,
        //   cancelColor: '#333',
        //   confirmText: '好的',
        //   confirmColor: '#d0a76c',
        //   success: function(e) {
        //     if (e.confirm) {
        //       wx.redirectTo({
        //         url: '../selectIdentity/selectIdentity',
        //       })
        //     }
        //   },
        // })
      } else {
        let that = this;
        wx.request({
          url: app.globalData.url + 'api/mine/index',
          data: {
            // token: this.globalData.token,
            token: wx.getStorageSync('token'),
          },
          method: 'POST',
          success: function(e) {
            // console.log(e.data.data.register);

            var register_info = e.data.data.register;

            //昵称
            // that.register_info.nickname = register_info.nickname;
            wx.setStorageSync('reg_nickname', register_info.nickname)
            //手机号
            // that.register_info.telephone = register_info.telephone;
            wx.setStorageSync('reg_telephone', register_info.telephone)
            //性别
            // that.register_info.sex = register_info.sex;
            wx.setStorageSync('reg_sex', register_info.sex)
            //头像地址
            // that.register_info.avatar_url = register_info.avatar_url;
            wx.setStorageSync('reg_avatar_url', register_info.avatar_url)
            //用户角色
            // that.register_info.register_role = register_info.register_role;
            wx.setStorageSync('reg_register_role', register_info.register_role)
            //签名
            // that.register_info.signature = register_info.signature;
            wx.setStorageSync('reg_signature', register_info.signature)
            //邀请码
            // that.register_info.invite_code = register_info.invite_code;
            wx.setStorageSync('reg_invite_code', register_info.invite_code)
            //微信Id
            // that.register_info.wxinfo_id = register_info.wxinfo_id;
            wx.setStorageSync('reg_wxinfo_id', register_info.wxinfo_id)
          },
        })
        // that.setData({
        //   avatar_url: wx.getStorageSync('reg_avatar_url'),
        //   nickname: wx.getStorageSync('reg_nickname'),
        //   signature: wx.getStorageSync('reg_signature'),
        //   sex: wx.getStorageSync('reg_sex')
        // })
        wx.hideLoading()
      }

    }

    this.setData({
      avatar_url: wx.getStorageSync('reg_avatar_url'),
      nickname: wx.getStorageSync('reg_nickname'),
      signature: wx.getStorageSync('reg_signature'),
      sex: wx.getStorageSync('reg_sex')
    })
    // console.log(this.data.avatar_url)
    var role = wx.getStorageSync('reg_register_role')
    // console.log(role)
    switch (role) {
      case 1:
        // console.log('会长')
        this.setData({
          role: 1
        })
        break;
      case 2:
        // console.log('主播')
        this.setData({
          role: 2
        })
        break;
      case 3:
        // console.log('管理员')
        this.setData({
          role: 3
        })
        break;
    }

    if (app.globalData.token) {
      this.getGuild();
    }

    wx.hideLoading()
  }
})